require 'fileutils'
class MockUploader
  attr_accessor :original_filename,:string
  def initialize(name,val)
    @original_filename = name
    @string = val
  end
  def read
    return @string
  end
end
def load_config
  YAML.load_file(File.join("config","configuration.yml"))
end
def sync_problem contest_id,problem_code,contest_path
  path = File.join(contest_path,problem_code)
  if !FileTest.exists? File.join(path,"problem-info.conf")
    puts "-|problem #{problem_code} has no problem-info.conf file"
  else
    conf_string = File.open(File.join(path,"problem-info.conf")).read
    a = conf_string.split(/\n/)
    h = {"letter" => "A"}
    a.each do |i|  
      temp = i.split("=")
      h[temp[0]] = temp[1]
    end
    if !Problem.find(:first,:conditions => {:contest_id => contest_id,:name => h["name"]})
      add_problem_to_contest contest_id,problem_code,contest_path
    else
      puts " |problem #{h["name"]} already exists"
    end
  end
end
def add_problem_to_contest contest_id,problem_code,contest_path
  config = load_config
  path = File.join(contest_path,problem_code)
  description = nil
  if FileTest.exists?(File.join(path,"description.pdf"))
    description = MockUploader.new("description.pdf",File.open(File.join(path,"description.pdf"),"rb").read)
  end
  if FileTest.exists?(File.join(path,"description.doc"))
    description = MockUploader.new("description.doc",File.open(File.join(path,"description.doc"),"rb").read)
  end
  answers = nil
  if FileTest.exists?(File.join(path,config["answer_file"]))
    answers = MockUploader.new(config["answer_file"],File.open(File.join(path,config["answer_file"]),"rb").read)
  end
  tests = nil
  if FileTest.exists?(File.join(path,config["test_file"]))
    tests = MockUploader.new(config["test_file"],File.open(File.join(path,config["test_file"]),"rb").read)
  end
  conf_string = File.open(File.join(path,"problem-info.conf")).read
  a = conf_string.split(/\n/)
  h = {"letter" => "A"}
  a.each do |i|  
    temp = i.split("=")
    h[temp[0]] = temp[1]
  end
  p = Problem.new(h)
  c = Contest.find_by_contest_id contest_id
  p.contest = c
  p.description = description
  p.answers = answers
  p.tests = tests
  if p.save
    puts "+|problem #{problem_code}"
  else
    puts "-|problem #{problem_code} not added due to:"
    puts p.errors.full_messages.map {|e| "  |" + e}
  end
end
def add_contest path,contest_set_code
  u  = File.join(path,contest_set_code)
  if !FileTest.exists?(File.join(u,"set-info.conf"))
    puts "-contest #{contest_set_code} no set-info.conf file" 
    return 
  else
    conf_string = File.open(File.join(u,"set-info.conf")).read
    a = conf_string.split(/\n/)
    h = {"set_code" => contest_set_code}
    a.each do |i|  
      temp = i.split("=")
      h[temp[0]] = temp[1]
    end
    c = Contest.new(h)
    if c.save
      puts "+contest: #{contest_set_code}"
    else
      puts "-contest #{contest_set_code} not added due to the reasons:"
      puts c.errors.full_messages.map{|e| "|" + e}
      return 
    end
    sync_contest(path,contest_set_code)
  end
end
def sync_contest path,contest_set_code
  u  = File.join(path,contest_set_code)
  c = Contest.find_by_set_code contest_set_code
  contest_dir = Dir.new(u)
  contest_dir.each do |contest_problem|
    unless contest_problem =~ /^\..*/
      if File.directory?(File.join(contest_dir.path,contest_problem))
        unless Problem.find(:first, :conditions => {:name => contest_problem,:contest_id => c.contest_id})
          sync_problem(c.contest_id,contest_problem,u) 
        end
      end
    end
  end
end
desc "Synchronizes the sets put in the sets directory adding them to the system"
task :sync_sets => ["environment"] do
  config = YAML.load_file(File.join("config","configuration.yml"))
  d = Dir.new File.join(config['base_dir'],'sets')
  d.each do |t|
    unless t=~ /^\..*/
      u = File.join(d.path,t)
      if File.directory?(u)
        if !Contest.find_by_set_code(t) #The contest is not added, yet
          add_contest(d.path,t)
        else
          puts ".contest #{t} already exists"
          sync_contest(d.path,t)
        end
      end
    end    
  end  
end